<template>
  <div>
    <el-card>
      <template #header>
        <div>
          <span>请假流程测试</span>
        </div>
      </template>
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="流程编码"> <span>{{ form.wfCode }}</span> </el-form-item>
        <el-form-item label="请假天数">
          <el-input-number v-model="form.leaveDays" :min="1" :max="10" label="请假天数"></el-input-number>
        </el-form-item>
        <el-form-item label="后台启动">
          <el-button type="primary" @click="onSubmit">直接提交</el-button>
          <el-button type="primary" @click="onSelectAndSubmit">选择处理人后再提交</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <select-next-user :selectUserParam="selectUserParam" :visible="selectUserOpen" @selected="doSelected" @close="selectUserOpen=false" />
  </div>
</template>

<script>
import { post } from '@/utils/ajax'
import { assign } from 'lodash-es';

export default {
  data() {
    return {
      form: {
        leaveDays: '1',
        wfCode: 'SAMPLES_LEAVE',
      },
      selectUserParam: {},
      selectUserOpen: false,
    }
  },
  methods: {
    // 选中用户
    doSelected(candidates) {
      this.selectUserOpen = false;

      var params = this.selectUserParam;
      params.candidates = candidates.join(',');
      post("/samples/leave/startWfInstanceAndCompleteFirst", params).then(response => {
        this.msgSuccess("请假申请提交成功：流程实例ID=" + response.data);
      });
    },
    // 直接调用业务系统后台Controller
    onSubmit() {
      const params = {
        leaveDays: this.form.leaveDays,
      };

      post("/samples/leave/startWfInstanceAndCompleteFirst", params).then(response => {
        this.msgSuccess("请假申请提交成功：流程实例ID=" + response.data);
      });
    },
    // 选择下一步处理人并调用业务系统后台Controller
    onSelectAndSubmit() {
      const params = {
        leaveDays: this.form.leaveDays,
        completeFirst: true,
      };
      const wfCode = this.form.wfCode;
      this.wfClient.isNeedSelectUser(wfCode, null, null, null, params, () => {
        //选择下一步处理
        var dest = { wfCode: wfCode };
        assign(dest, dest, params);
        this.selectUserParam = dest;
        this.selectUserOpen = true;
      }, () => {
        //直接启动流程
        this.onSubmit();
      });
    },
  }
}
</script>

<style lang="scss" scoped>
</style>